package com.atguigu.mall.mallproduct.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.atguigu.mall.mallproduct.entity.PmsBrandEntity;
import com.atguigu.mall.mallproduct.vo.BrandVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.mall.mallproduct.entity.PmsCategoryBrandRelationEntity;
import com.atguigu.mall.mallproduct.service.PmsCategoryBrandRelationService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

/**
 * 品牌分类关联
 *
 * @author Fuc
 * @email leftbear@gmail.com
 * @date 2020-10-21 14:01:49
 */
@RestController
@RequestMapping("mallproduct/pmscategorybrandrelation")
public class PmsCategoryBrandRelationController {
  @Autowired private PmsCategoryBrandRelationService pmsCategoryBrandRelationService;

  /**
   *
   * @param brandId
   * @return
   */
  @GetMapping("/catelog/list")
  public R catelogList(@RequestParam("brandId") Long brandId) {
    QueryWrapper<PmsCategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
    List<PmsCategoryBrandRelationEntity> data = pmsCategoryBrandRelationService.list(queryWrapper);
    return R.ok().put("data", data);
  };

  /** 列表 */
  @RequestMapping("/list")
  public R list(@RequestParam Map<String, Object> params) {
    PageUtils page = pmsCategoryBrandRelationService.queryPage(params);

    return R.ok().put("page", page);
  }

  /** 信息 */
  @RequestMapping("/info/{id}")
  public R info(@PathVariable("id") Long id) {
    PmsCategoryBrandRelationEntity pmsCategoryBrandRelation =
        pmsCategoryBrandRelationService.getById(id);

    return R.ok().put("pmsCategoryBrandRelation", pmsCategoryBrandRelation);
  }

  /** 保存 */
  @RequestMapping("/save")
  public R save(@RequestBody PmsCategoryBrandRelationEntity pmsCategoryBrandRelation) {
    pmsCategoryBrandRelationService.save(pmsCategoryBrandRelation);

    return R.ok();
  }

  /** 修改 */
  @RequestMapping("/update")
  public R update(@RequestBody PmsCategoryBrandRelationEntity pmsCategoryBrandRelation) {
    pmsCategoryBrandRelationService.updateById(pmsCategoryBrandRelation);

    return R.ok();
  }

  /** 删除 */
  @RequestMapping("/delete")
  public R delete(@RequestBody Long[] ids) {
    pmsCategoryBrandRelationService.removeByIds(Arrays.asList(ids));

    return R.ok();
  }


  /**
   *  /product/categorybrandrelation/brands/list
   *
   *  1、Controller：处理请求，接受和校验数据
   *  2、Service接受controller传来的数据，进行业务处理
   *  3、Controller接受Service处理完的数据，封装页面指定的vo
   */
  @GetMapping("/brands/list")
  public R relationBrandsList(@RequestParam(value = "catId",required = true)Long catId){
    List<PmsBrandEntity> vos = pmsCategoryBrandRelationService.getBrandsByCatId(catId);

    List<BrandVo> collect = vos.stream().map(item -> {
      BrandVo brandVo = new BrandVo();
      brandVo.setBrandId(item.getBrandId());
      brandVo.setBrandName(item.getName());

      return brandVo;
    }).collect(Collectors.toList());

    return R.ok().put("data",collect);

  }

}
